package com.obsidian.free.slideshow;

import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
   Vector<Bitmap> views;
   Context context;
   
   ImageAdapter(Context context) {
      super();
      views = new Vector<Bitmap>();
      this.context = context;
   }
   
   ImageAdapter(Context context, Bitmap[] contents) {
      super();
      views = new Vector<Bitmap>();
      for(Bitmap bm : contents) {
         views.add(bm);
      }
      this.context = context;
   }
   
   public void add(Bitmap image) {
      views.add(image);
   }
   
   public void clear() {
      views.clear();
   }
   
   public void deletePosition(int position) {
      views.remove(position);
   }
   
   public int getCount() {
      return views.size();
   }

   public Object getItem(int position) {
      return views.get(position);
   }

   public long getItemId(int position) {
      return 0;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      ImageView view = null;
      if(convertView == null) {
          view = new ImageView(context);
          view.setLayoutParams(new GridView.LayoutParams(85, 85));
          view.setScaleType(ImageView.ScaleType.FIT_CENTER);
          view.setPadding(2, 2, 2, 2);
      }else {
          view = (ImageView)convertView;
      }
      view.setImageBitmap(views.get(position));
      return view;
   }
   
   public Bitmap[] toArray() {
      Bitmap[] array = new Bitmap[views.size()];
      for(int i = 0; i < views.size(); i++) {
         array[i] = views.get(i);
      }
      return array;
   }
}
